﻿using System.Globalization;
using System.Reflection;
using System.Windows;
using LukasKopenec.LocalizationSupport;
using LukasKopenec.LocalizationSupport.Providers;

namespace Notoric.Gui
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        #region Event Handlers

        private void Application_Exit(object sender, ExitEventArgs e)
        {
            Notoric.Gui.Properties.Settings.Default.Save();
        }

        private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            MessageBox.Show(string.Format("An unhandled exception occured\n{0}\n{1}", e.Exception.Message, e.Exception.StackTrace));
            e.Handled = true;
        }

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            System.IO.Directory.SetCurrentDirectory(System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments));

            string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            var provider = new ResourceFileProvider(
                "Localizations.Resources",
                Assembly.LoadFrom(System.IO.Path.Combine(path, "Localizations.dll"))
                );

            var defaultCulture = new CultureInfo("en-US");
            LocalizationManager.SupportedCultures.Add(defaultCulture, provider);
            LocalizationManager.CurrentCulture = defaultCulture;

            if (e.Args.Length > 0)
                this.Properties["CommandLine"] = e.Args;

            View.Music.CWNotation.ApplicationBridge.CreateInstance(CurrentState);
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Holds the current application state.
        /// </summary>
        public ApplicationState CurrentState
        {
            get { return currentState; }
        }

        #endregion

        #region Private Fields

        /// <summary>
        /// Holds the current application state.
        /// </summary>
        private ApplicationState currentState = new ApplicationState();

        #endregion
    }
}